<?php

namespace Application\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Application\Model\Demandes;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream;

class DemandesTable extends AbstractTableGateway {

	protected $table ='demandes';
	protected $tablename ='demandes';
	var  $logger = null;
	var  $writer = null;
	
	var $dossier ='../lamisa.burundi/public/img/avatar/';
	
		
	public function __construct(Adapter $adapter) {
		$this->adapter = $adapter;
		$this->resultSetPrototype = new ResultSet(new Demandes());
		$this->initialize();
		
		
	}

	
	public function fetchAll()
	{
		$resultSet = $this->select();
		return $resultSet;
	}

	public function getDemandes($id)
	{
		$logger->info('Methode Demandes');
		
		$id = (int) $id;
		$rowset = $this->select(array('id_demande' => $id
								));
		//$row = $rowset->current();
		if (!$rowset) {
			//throw new \Exception("Could not find row $id");
			$rowset = "Error";
		}
		return $rowset;
	}

	public function getDemandesByUserId($id_user)
	{
		$stream = @fopen('fichiers/logs/'.date('Y-m-d').'_log.log', 'a', false);
		$writer = new Stream($stream);
		$logger = new Logger();
		$logger->addWriter($writer);
		$logger->info('Methode getDemandesByUserId '.$id_user.'O k');
		$id = (int) $id_user;
		$rowset = $this->select(array('id_user' => $id
		));
		//$row = $rowset->current();
		if (!$rowset) {
			//throw new \Exception("Could not find row $id");
			$rowset = "Error";
		}
		return $rowset;
	}
	
	public function addDemande($id_user, $type_demande,$demande,$date_demande, $auteur, $flag_urgent,$flag_traite,$code_reponse,$date_traitement)
	{
	
		$data = array(
				'id_user' => $id_user,
				'type_demande' => $type_demande,
				'demande' => $demande,
				'date_demande' => $date_demande,
				'auteur' => $auteur,
				'flag_urgent' => $flag_urgent,
				'flag_traite' => $flag_traite,
				'code_reponse' => $code_reponse,
				'date_traitement' => $date_traitement,
		);
		$this->insert($data);
	}
	
	/*
	public function getTacheByuser($id)
	{
		$stream = @fopen('fichiers/logs/'.date('Y-m-d').'_log.log', 'a', false);
		$writer = new Stream($stream);
		$logger = new Logger();
		$logger->addWriter($writer);
				
		$logger->info('Methode get TacheName');
		if (! $stream) {
			throw new Exception('Failed to open stream');
		}
		

		$rowset = $this->select(array('login' => $name));
		$row = $rowset->current();
		if (!$row) {
			//throw new \Exception("Could not find $name ");
			$logger->err("Could not find $name ");
			;
		}
		return $row;
	}
	
	
	
	public function addPersonnel($nom, $prenom,$login,$fonction,$email, $telephone, $password,$adresse,$date_inscrip)
	{
		
		$data = array(
				'nom' => $nom,
				'prenom' => $prenom,
				'login' => $login,
				'fonction' => $fonction,
				'email' => $email,
				'telephone' => $telephone,
				'password' => $password,
				'adresse' => $adresse,
				'salaire' => "",
				'contrat' => "",
				'date_embauche' => $date_inscrip,
		);
		$this->insert($data);
	}
	*/
	
/*	
	public function savePersonnel(Personnel $user)
	{
		$data = array(

				'username' => $user->username,
				'id_personnel' => $user->id_personnel,
				'password' => $user->password,
				'email' => $user->email,
				'avatar' => $user->avatar,
				'isadmin' => $user->isadmin,
				'isblocked' => $user->isblocked,
				'signup_date' => $user->signup_date,
		);
		$id = (int)$user->id_user;
		if ($id == 0) {
			$this->insert($data);
		} else {
			if ($this->getUser($id)) {
				$this->update($data, array('id_user' => $id));
			} else {
				throw new \Exception('Form id does not exist');
			}
		}
	}
*/
	
	//TODO
	/*public function addFile($id_user,$nom_fichier,$url,$type_fichier,$date_upload)
	{
		$data = array(
				//'id_file' => $id_file,
				'id_user' => $id_user,
				'nom_fichier' => $nom_fichier,
				'url' => $url,
				'type_fichier' => $type_fichier,
				'date_upload' => $date_upload,			
		);
		$this->insert($data);
	}
*/

	public function updateDemande($reponse,$id_demande)
	{
		
		$stream = @fopen('fichiers/logs/'.date('Y-m-d').'_log.log', 'a', false);
		$writer = new Stream($stream);
		$logger = new Logger();
		$logger->addWriter($writer);
		$logger->info('Methode updateDemande reponse: '.$reponse );
		$logger->info('Methode updateDemande id_demande: '.$id_demande );
		
		$date =date("Y-m-d");
		$data = array(
				//'id_user' => $id_user,
				'flag_traite' => 1,
				'code_reponse' => $reponse,
				'date_traitement' => $date,
		);
		$this->update($data, array('id_demande' => $id_demande));
		echo $id_demande;
		
	//	$this->update($data, array('id_tache' => $id_tache));

	}
/*	public function deleteAlbum($id)
	{
		$this->delete(array('id_user' => $id));
	}
	
	*/
}

?>
